<template>
  <div>
    <!--查询表单-->
    <el-form :inline="true" class="demo-form-inline">
      <el-button type="primary" @click="toAdd">新增</el-button>
      <el-form-item>
        <el-input placeholder="用户名" />
      </el-form-item>
      <el-form-item>
        <el-input placeholder="手机号" />
      </el-form-item>
      <el-form-item label="添加时间">
        <el-date-picker
          type="datetime"
          placeholder="选择开始时间"
          value-format="yyyy-MM-dd HH:mm:ss"
          default-time="00:00:00"
        />
      </el-form-item>
      <el-button type="primary" icon="el-icon-search" @click="getList()">查询</el-button>
      <el-button type="default" @click="resetData()">清空</el-button>
    </el-form>
    <!-- 表格 -->
    <el-table :data="address">
      <el-table-column label="编号" type="index" :index="1" align="center"></el-table-column>
      <el-table-column prop="user.realname" label="用户名" align="center"></el-table-column>
      <el-table-column prop="username" label="手机号" align="center"></el-table-column>
      <el-table-column prop="telephone" label="电话" align="center"></el-table-column>
      <el-table-column prop="province" label="省份" align="center"></el-table-column>
      <el-table-column prop="city" label="城市" align="center"></el-table-column>
      <el-table-column prop="area" label="区" align="center"></el-table-column>
      <el-table-column prop="address" label="详细地址" align="center"></el-table-column>
      <el-table-column fixed="right" label="操作" align="center">
        <template slot-scope="scope">
          <el-button type="danger" @click="del(scope.row.id)" size="mini">删除</el-button>
          <el-button type="primary" @click="edit(scope.row)" size="mini">编辑</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <el-pagination
      :current-page="page"
      :page-size="limit"
      :total="total"
      style="padding: 30px 0; text-align: center;"
      layout="total, prev, pager, next, jumper"
      @current-change="get"
    />
    <!-- 表格结束 -->
    <!-- 模态框 -->
    <el-dialog title="用户" :visible.sync="visable">
      <el-form ref="ruleForm" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="收货人" prop="username">
          <el-input v-model="form.username" clearable placeholder="请输入收货人" />
        </el-form-item>
        <el-form-item label="手机号" prop="telephone">
          <el-input
            v-model="form.telephone"
            clearable
            placeholder="请输入手机号"
            maxlength="11"
            show-word-limit
          />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="close('ruleForm')">取消</el-button>
        <el-button type="primary" @click="submit('ruleForm')">确定</el-button>
      </div>
    </el-dialog>
    <!-- 模态框结束 -->
  </div>
</template>
<script>
import { get, post } from '@/utils/request'
export default {
  data() {
    return {
      text: '',
      visable: false,
      form: {},
      address: [],
      page: 1, //当前页
      limit: 10, //每页记录数
      total: 4 //总记录数
    }
  },
  created() {
    this.loadAddress()
  },
  methods: {
    loadAddress() {
      let url = 'http://47.93.12.6:8889/address/findAllWithUser'
      get(url).then(response => {
        this.address = response.data
      })
    },
    del(id) {
      this.$confirm('此操作将永久删除该数据, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        // 删除
        let url = 'http://47.93.12.6:8889/address/deleteById'
        get(url, { id: id }).then(response => {
          // 提示
          this.$message({ type: 'success', message: response.message })
          // 刷新数据
          this.loadAddress()
        })
      })
    },
    submit(form) {
      this.$refs[form].validate(valid => {
        if (valid) {
          // 表单验证成功的情况
          let url = 'http://47.93.12.6:8889/address/saveOrUpdate'
          post(url, this.form).then(response => {
            this.$message({ type: 'success', message: response.message })
            this.visable = false
            this.loadAddress()
            this.$refs[form].resetFields()
          })
        } else {
          return false
        }
      })
    },
    close(form) {
      this.visable = false
      this.$refs[form].resetFields()
    },
    edit(row) {
      this.form = row
      this.visable = true
    }
  }
}
</script>

<style>
</style>